package com.bobi.tfca.ui.mainnavigation.home.rules

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.lifecycle.ViewModel
import com.bobi.tfca.base.BaseFragment
import com.bobi.tfca.constants.PageElementIdConstant
import com.bobi.tfca.databinding.FragmentAssRulesBinding
import com.bobi.tfca.network.ApiResponse
import com.bobi.tfca.network.api.ResourceAPI
import com.bobi.tfca.util.LogUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.MediaType
import okhttp3.RequestBody

class AssRulesFragment : BaseFragment<FragmentAssRulesBinding, ViewModel>(
    FragmentAssRulesBinding::inflate,
    null,
    true
) {

    @SuppressLint("ResourceType", "UseRequireInsteadOfGet")
    override fun initFragment(
        binding: FragmentAssRulesBinding,
        viewModel: ViewModel?,
        savedInstanceState: Bundle?
    ) {
        // 设置修改按钮监听
        binding.ivEdit.setOnClickListener {
            // 隐藏修改按钮，显示保存按钮
            binding.ivEdit.visibility = View.GONE
            binding.btSave.visibility = View.VISIBLE
            // 隐藏tv,显示et
            binding.svContent.visibility = View.GONE
            binding.svEditContent.visibility = View.VISIBLE
        }

        // 设置保存按钮监听
        binding.btSave.setOnClickListener {
            val change = binding.etContent.text.toString()
            saveAssRulesChange(change, binding)
        }

        // 设置菜单为监听的
        publicViewModel!!.apply {
            visiblePageElements.observe(viewLifecycleOwner) {
                binding.ivEdit.visibility = getElementVisibility(PageElementIdConstant.FRAGMENT_ASS_RULES_IV_EDIT)
            }
        }

        getAssRules(binding)
    }

    private fun saveAssRulesChange(change: String, binding: FragmentAssRulesBinding) {
        val requestBody = RequestBody.create(MediaType.parse("text/plain"), change)
        publicViewModel!!.apply {
            request(ResourceAPI::class.java).saveAssRulesChange(requestBody).getResponse {
                it.collect {
                    when (it) {
                        is ApiResponse.Error -> {
                            LogUtil.e(it.errMsg)
                        }

                        ApiResponse.Loading -> LogUtil.e("Loading")
                        is ApiResponse.Success -> {
                            withContext(Dispatchers.Main) {
                                binding.tvAssRules.text = change
                                binding.etContent.setText(change)
                                // 隐藏保存按钮，显示修改按钮
                                binding.btSave.visibility = View.GONE
                                binding.ivEdit.visibility = View.VISIBLE
                                // 显示tv,隐藏et
                                binding.svEditContent.visibility = View.GONE
                                binding.svContent.visibility = View.VISIBLE
                            }
                        }
                    }
                }
            }
        }
    }

    private fun getAssRules(binding: FragmentAssRulesBinding) {
        publicViewModel!!.apply {
            request(ResourceAPI::class.java).getAssRules().getResponse {
                it.collect {
                    when (it) {
                        is ApiResponse.Error -> {
                            LogUtil.e(it.errMsg)
                        }

                        ApiResponse.Loading -> LogUtil.e("Loading")
                        is ApiResponse.Success -> {
                            withContext(Dispatchers.Main) {
                                val result = it.data?.data
                                if (result != null) {
                                    binding.tvAssRules.text = result
                                    binding.etContent.setText(result)
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}